<cfcomponent output="false">

	<!--- --->
	
	<cffunction name="saveEvent" access="public" output="true" returntype="boolean">
		<cfargument name="requested_date" required="false" default="#$.date.getDate()#" />
		<cfargument name="redirect" required="true" />

		<cfset var local = {} />
				
		<cfset $.run.it('calendarService.saveEvent',arguments) />
		
		<cfif arguments.redirect>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.requested_date#" addToken="no" />
		<cfelse>
			<cflocation url="#cgi.http_referer#" addToken="no" />
		</cfif>

		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteEvent" access="public" output="true" returntype="boolean">
		<cfargument name="eventID" required="true" />
		<cfargument name="requested_date" required="true" />
		<cfargument name="redirect" required="true" />

		<cfset $.run.it('calendarService.deleteEvent',arguments) />

		<cfif arguments.redirect>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.requested_date#" addToken="no" />
		<cfelse>
			<cflocation url="#cgi.http_referer#" addToken="no" />
		</cfif>
		
		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="connectToCalendar" access="public" output="true" returntype="boolean">
		<cfargument name="date" required="true" />
		<cfargument name="code" required="true" hint="google api's authorization code" />
		
		<cfset var local = {} />
		
		<!--- attempt to obtain refresh token --->
		<cfif arguments.code neq "">
			<cfset local.result = $.run.it('googleService.connectToCalendar',arguments) />
			<cfif local.result.success>
				<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#" addToken="no" />
			<cfelse>
				<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#&error=googleCalendarAPIError" addToken="no" />
			</cfif>
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#&error=failedToGetPermissionForGoogleCalendar" addToken="no" />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="disconnectCalendar" access="public" output="true" returntype="boolean">
		<cfargument name="date" required="true" />
		<cfargument name="refresh_token" required="true" />
	
		<cfset var local = {} />
		
		<!--- attempt to obtain refresh token --->
		<cfset local.result = $.run.it('googleService.disconnectCalendar',arguments) />
		
		<cfif local.result>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#" addToken="no" />
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#&error=FailedToDisconnect" addToken="no" />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="refreshGoogleCalendar" access="public" output="true" returntype="boolean">
		<cfargument name="date" required="true" />
		
		<!--- attempt to obtain refresh token --->
		<cfset $.run.thread('googleService.syncCalendar') />

		<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="resyncGoogleCalendar" access="public" output="true" returntype="boolean">
		<cfargument name="date" required="false" default="" />
		<!--- attempt to obtain refresh token --->
		<cfset $.run.it('googleService.fullSync') />
		
		<cflocation url="#$.site.getVar('rootURL')#/craftivity/calendar.cfm?date=#arguments.date#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="connectGoogleCalendar" access="public" output="true" returntype="boolean">
		
		<cfset var local = {} />
		
		<cfset local.url = 'https://accounts.google.com/o/oauth2/auth?' />
		<cfset local.url = $.link.setArg(local.url,'scope','https://www.googleapis.com/auth/calendar') />
		<cfset local.url = $.link.setArg(local.url,'state','#arguments.date#') />
		<cfset local.url = $.link.setArg(local.url,'redirect_uri',$.site.getVar('google_redirect_uri')) />
		<cfset local.url = $.link.setArg(local.url,'response_type','code') />
		<cfset local.url = $.link.setArg(local.url,'client_id',$.site.getVar('google_client_id')) />
		<cfset local.url = $.link.setArg(local.url,'access_type','offline') />
		<cfset local.url = $.link.setArg(local.url,'approval_prompt','force') />

		<cflocation url="#local.url#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveCalendarOptions" access="public" output="true" returntype="void">
		
		<cfset var local = {} />
		
		<cfset local.key = arguments.key />
		<cfset local.oldCalendarIDs = arguments.oldIDs />
		
		<cfset $.run.it('calendarService.saveEventCategories',{categories=arguments.categories}) />
		
		<cfset structDelete(arguments,"categories") />
		<cfset structDelete(arguments,"key") />
		<cfset structDelete(arguments,"$") />
		<cfset structDelete(arguments,"$gateway") />
		<cfset structDelete(arguments,"$view") />
		<cfset structDelete(arguments,"$service") />
		<cfset structDelete(arguments,"$controller") />
		<cfset structDelete(arguments,"fieldnames") />
		<cfset structDelete(arguments,"cfc") />
		<cfset structDelete(arguments,"method") />
		<cfset structDelete(arguments,"oldIDs") />

		<cfset $.run.it('googleService.updateCalendarSelected',{oldIDs=local.oldCalendarIDs,newIDs=arguments.google_calendar_ids}) />
		<cfset $.run.it('optionService.saveOptions',{key=local.key,args=arguments}) />

		<cflocation url="#cgi.http_referer#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
</cfcomponent>